/******************************************************************************
 * Copyright (C), 2000-2014,  Frank.
 * filename: ir.c
 * Author: Frank
 * Version:
 * Data: 2014-12-10 
 * brief:
 ******************************************************************************/

#include <CoOS.h>			                 /*!< CoOS header file            */
#include <stdio.h>
#include "NUC123.h"

#define IR_MAX_PLUS		10000
#define IR_MAX_CHECK	200000
#define IR_TASK_SIZE	64

OS_STK     taskIR_stk[IR_TASK_SIZE];

U8 u8CurLevel;
U8 u8Drop;

void TMR2_IRQHandler(void)
{
	//Capture Int;
	if(TIMER_GetCaptureIntFlag(TIMER2) == 1)
	{
		U32 u32Plus = TIMER_GetCaptureData(TIMER2);
		TIMER_Reset(TIMER2);
		TIMER_ClearCaptureIntFlag(TIMER2);		
		if(u8Drop == 0)
		{
			
			//printf("u32Plus %d\r\n", u32Plus);
			if(u32Plus <= IR_MAX_PLUS)
			{
				Blast_WritePlus(u32Plus);
			}
		}
		else
		{
			u8Drop = 0;
		}
	}

	//timer Int
	if(TIMER_GetIntFlag(TIMER2) == 1)
	{
		TIMER_ClearIntFlag(TIMER2);
		if(u8CurLevel == PB2)
		{
			u8Drop = 1;
		}
	}
}

void IR_Task (void* pdata)
{
#if 0
	U8 u8Protocol;	
	bool bDone = FALSE;
	bool bExitFlag = FALSE;
	U16 u16Num = 0;
	U32 u32Delay = 0;
	U32 u32Code;
	U64 u64ExitTime = 0;
	
	TIMER_Start(TIMER2);
	while(1)
	{
		u16Num = Blast_GetPlusNum();
		if(u16Num > 0)
		{
			bDone = Blast_DoDecode(&u8Protocol, &u32Code);
			if(bDone == TRUE)
			{
				//Get Key
				bDone = FALSE;
				bExitFlag = FALSE;
				u32Delay = 10;
			}
			else
			{
				u32Delay = 10;
				if(bExitFlag == FALSE)
				{
					u64ExitTime = CoGetOSTime();
					bExitFlag = TRUE;
				}
				else
				{
					if(CoGetOSTime() - u64ExitTime > 100)
					{
						bExitFlag = FALSE;
						Blast_ExitDecode();
						u32Delay = 100;
					}
				}
			}
		}
		else
		{
			if(bExitFlag == TRUE)
			{
				if(CoGetOSTime() - u64ExitTime > 200)
				{
					bExitFlag = FALSE;
					Blast_ExitDecode();
					u32Delay = 100;
				}
			}
			u32Delay = 100;
		}

		CoTickDelay(u32Delay);
	}
#else
	while(1)
	{
		
		U16 u16Num = Blast_GetPlusNum();
		//printf("Plus number %d\r\n", u16Num);
		U16 i;
		U32 u32Plus;
		if(u16Num >= 21)
		{
			for(i = 0; i<u16Num; i++)
			{			
				if(Blast_ReadPlus(&u32Plus))
				{
					printf("%02d: %08d\r\n", i, u32Plus);
				}
				else
				{
					break;
				}
				
			}
		}
		CoTickDelay(1000);
	}
#endif
}


 void IR_Init(void)
 {
	 /* Initial Timer2 default setting */
	 TIMER_Open(TIMER2, TIMER_PERIODIC_MODE, 1);

	/* Configure Timer2 setting for external counter input and capture function */
	TIMER_ClearCaptureIntFlag(TIMER2);
	TIMER_ClearIntFlag(TIMER2);
	TIMER_ClearWakeupFlag(TIMER2);
	
	TIMER_SET_PRESCALE_VALUE(TIMER2, 11);
	TIMER_SET_CMP_VALUE(TIMER2, 1000000);
	TIMER_EnableCaptureDebounce(TIMER2);
	TIMER_EnableCapture(TIMER2, TIMER_CAPTURE_FREE_COUNTING_MODE, TIMER_CAPTURE_FALLING_AND_RISING_EDGE);
	TIMER_EnableCaptureInt(TIMER2);
	TIMER_EnableInt(TIMER2);

	u8CurLevel = PB2;
	u8Drop = 1; //drop first;
	/* Enable Timer2 NVIC */
	NVIC_EnableIRQ(TMR2_IRQn);
	
	TIMER_Start(TIMER2);
	CoCreateTask(IR_Task, 0, 3, taskIR_stk, IR_TASK_SIZE);
 }

